﻿using Rhino.Mocks;
using System.Linq;
using StreetPacMan.Server.Tests.Integ;
using StructureMap;

namespace StreetPacMan.Server.Tests
{
    public static class ServerFacadeFactory
    {
        public static IServerFacade GetServerFacade()
        {
            ObjectFactory.Inject(ConnectionProviderFactory.GetConnectionProvider());
            return ObjectFactory.GetInstance<IServerFacade>();
        }

        public static IServerFacade CreateGameWithApplesOnlyAt(params InitialApple[] apples)
        {
            var applesProvider = MockRepository.GenerateStub<IInitialAppleLocationsProvider>();
            applesProvider.Stub(x => x.GetInitialApples()).Return(apples);

            ObjectFactory.Inject(applesProvider);
            ObjectFactory.Inject(ConnectionProviderFactory.GetConnectionProvider());
            try
            {
                return ObjectFactory.GetInstance<IServerFacade>();
            }
            finally
            {
                ObjectFactory.Inject<IInitialAppleLocationsProvider>(new TestAppleLocationsProvider());
            }
        }

        public static IServerFacade CreateGameWithExtraApplesAt(params InitialApple[] apples)
        {
            var applesProvider = MockRepository.GenerateStub<IInitialAppleLocationsProvider>();
            applesProvider.Stub(x => x.GetInitialApples()).Return(new TestAppleLocationsProvider().GetInitialApples().Concat(apples).ToArray());

            ObjectFactory.Inject(applesProvider);
            ObjectFactory.Inject(ConnectionProviderFactory.GetConnectionProvider());
            try
            {
                return ObjectFactory.GetInstance<IServerFacade>();
            }
            finally
            {
                ObjectFactory.Inject<IInitialAppleLocationsProvider>(new TestAppleLocationsProvider());
            }
        }
    }
}